package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Resources;
import com.funmi.lizip.entity.dto.resources.ResourcesAddRequest;
import com.funmi.lizip.entity.dto.resources.ResourcesQueryRequest;
import com.funmi.lizip.entity.dto.resources.ResourcesUpdateRequest;
import com.funmi.lizip.entity.vo.ResourcesVo;
import com.funmi.lizip.service.ResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@Api(tags = "资源环境查询")
@RequestMapping("resources")
public class ResourcesController {

    @Autowired
    private ResourcesService  resourcesService;



    @PostMapping("")
    @ApiOperation(value = "专题图的新增")
    public R addResources(@RequestBody ResourcesAddRequest request){
        if(request==null){
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }
        Resources resources = new Resources();
        BeanUtils.copyProperties(request,resources);
        resources.setCreateAt(LocalDateTime.now());
        resources.setIsDelete(false);
        boolean save = resourcesService.save(resources);
        if(save){
            return R.success().message("新增成功");
        }else {
            throw new ApplicationException("新增失败");
        }

    }



    @ApiOperation(value = "专题图的修改")
    @PutMapping("/{id}")
    public R updateResources(@RequestBody(required = false) ResourcesUpdateRequest request){
        if(request==null || request.getId()==null){
            throw new ApplicationException(ResultCode.PARAMS_ERROR);
        }
        Resources resources = new Resources();
        BeanUtils.copyProperties(request,resources);

        boolean update = resourcesService.updateById(resources);
        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改成功");
        }
    }

    @ApiOperation(value = "专题图的删除")
    @DeleteMapping("/{id}")
    public R deleteResources(@PathVariable Integer id){
        if(id<=0){
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = resourcesService.removeById(id);
        if (result) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }


    @ApiOperation(value = "根据输入进行检索")
    @GetMapping("")
    public R pageList( ResourcesQueryRequest request){
        long current =1;
        long size = 5;
        Resources resourcesQuery = new Resources();
        if(request !=null){
            BeanUtils.copyProperties(request,resourcesQuery);
            current =request.getPage();
            size =request.getPageSize();
        }

        QueryWrapper<Resources> qw = new QueryWrapper<>(resourcesQuery);

        if(request!=null && request.getName()!=null){
            qw.eq("name",resourcesQuery.getName());
        }

        if(request!=null && request.getType()!=null){
            qw.eq("type",resourcesQuery.getType());
        }

        qw.select().orderByDesc("create_at");

        Page<Resources> resourcesPage = resourcesService.page(new Page<>(current, size), qw);

        PageDTO<ResourcesVo> resourcesVoPage = new PageDTO<>(resourcesPage.getCurrent(), resourcesPage.getSize(), resourcesPage.getTotal());

        List<ResourcesVo> resourcesVoList = resourcesPage.getRecords().stream().map(resources -> {
            ResourcesVo resourcesVo = new ResourcesVo();
            BeanUtils.copyProperties(resources, resourcesVo);
            return resourcesVo;
        }).collect(Collectors.toList());

        resourcesVoPage.setRecords(resourcesVoList);
        return R.success().data(resourcesVoPage);
    }
}
